package com.gopay.common.user.manager.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.gopay.common.domain.cust.CustCorpInfo;
import com.gopay.common.domain.cust.CustPersonInfo;
import com.gopay.common.user.dao.CustCorpInfoQueryDAO;
import com.gopay.common.user.manager.CustCorpInfoQueryManager;
import com.gopay.common.util.NonrevEncr;
import com.gopay.common.util.pwd.UserPwdUtil;

/**
 * CustCorpInfoQuery Manager实现
 * 
 * @CustCorpInfoQueryManagerImpl.java
 * @author liuxin
 * @2013-1-6 下午2:02:45 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("custCorpInfoQueryManager")
public class CustCorpInfoQueryManagerImpl implements CustCorpInfoQueryManager {

    @Resource(name = "custCorpInfoQueryDao")
    private CustCorpInfoQueryDAO custCorpInfoQueryDao;

    @Override
    public CustCorpInfo get(Serializable id) {
        return custCorpInfoQueryDao.find(CustCorpInfo.class, id);
    }

    @Override
    public List<CustCorpInfo> get(Map<Object, Object> paraMap) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<CustCorpInfo> getAll() {
        return custCorpInfoQueryDao.findAll(CustCorpInfo.class);
    }

    @Override
    public void updateMerchantInfo(String custId,String websiteUrl, String websiteName, String identityCode, String websiteDesc) {
        CustCorpInfo ci = custCorpInfoQueryDao.find(CustCorpInfo.class, custId);
        if(StringUtils.isNotBlank(websiteUrl)) {
            ci.setWebsiteUrl(websiteUrl);
        }
        if(StringUtils.isNotBlank(websiteDesc)) {
            ci.setWebsiteDesc(websiteDesc);
        }
        if(StringUtils.isNotBlank(websiteName)) {
            ci.setMerName(websiteName);
        }
        if(StringUtils.isNotBlank(identityCode)) {
            ci.setIdentityCode(identityCode);
        }
        ci.setLastUpdTs(new Date());
        
        custCorpInfoQueryDao.update(ci);
    }

    /* (non-Javadoc)
     * @see com.gopay.common.user.manager.CustCorpInfoQueryManager#getByEmail(java.lang.String)
     */
    @Override
    public CustCorpInfo getByEmail(String email) {
        return custCorpInfoQueryDao.findCustEmail(email);
    }

    /* (non-Javadoc)
     * @see com.gopay.common.user.manager.CustCorpInfoQueryManager#getCustCorpOpen(java.lang.String)
     */
    @Override
    public List<CustCorpInfo> getCustCorpOpen(String email) {
        return custCorpInfoQueryDao.findCustCorpOpen(email);
    }

	@Override
	public CustCorpInfo getByMobile(String mobile) {
		return custCorpInfoQueryDao.findCustMobile(mobile);
	}

	@Override
	public int getByCertTypeAndCertNo(String certType, String certNo) {
		return custCorpInfoQueryDao.findByCertTypeAndCertNo(certType, certNo);
	}

    @Override
    public CustCorpInfo getNoActiveCustByCustEmail(String email) {
        return custCorpInfoQueryDao.getNoActiveCustByCustEmail(email);
    }

    @Override
    public CustCorpInfo getNoActiveCustByCustMob(String mobile) {
        return custCorpInfoQueryDao.getNoActiveCustByCustMob(mobile);
    }

	@Override
	public CustCorpInfo getByCustId(String custId) {
		return custCorpInfoQueryDao.getByCustId(custId);
	}
	@Override
	public CustCorpInfo getByMobileNo(String MobileNo) {
		return custCorpInfoQueryDao.findByMobileNo(MobileNo);
	}

	
    @Override
    public List<CustCorpInfo> getCorpByCorpName(String corpName) {
        return custCorpInfoQueryDao.findAllByCorpName(corpName);
    }

	@Override
    public List<CustCorpInfo> getByEmailAndCustStat(String email, String[] custStat){
		return custCorpInfoQueryDao.getByEmailAndCustStat(email,custStat);
	}

    @Override
    public CustCorpInfo findByEmail(String email, String custName, String certType) {
        return custCorpInfoQueryDao.findByEmail(email,custName,certType);
    }

    @Override
    public CustCorpInfo findByMobile(String mob, String custName, String certType) {
        return custCorpInfoQueryDao.findByMobile(mob,custName,certType);
    }
    
    @Override
    public void setCorpNewPayPwd(StringBuilder sbNewPayPwd, CustCorpInfo custCorpInfo) {
        if(UserPwdUtil.isSHA1(custCorpInfo.getPayPwd())) {
            custCorpInfo.setPayPwd(NonrevEncr.sha512(sbNewPayPwd));
        } 
    }
    @Override
    public CustCorpInfo getByEmailWithoutStat(String email) {
        List<CustCorpInfo> list = this.custCorpInfoQueryDao.getByEmailWithoutStat(email);
        if(list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

	@Override
	public boolean isAirlineMerchant(String custId){
		CustCorpInfo custCorpInfo = this.getByCustId(custId);
		if(null == custCorpInfo){
			return false;
		}
		
		return "1".equals(custCorpInfo.getIsAirlineMerchant());
	}
	
	@Override
	 public CustCorpInfo findByMerCustId(String merId, String merCustId) {
		 List<CustCorpInfo> list = custCorpInfoQueryDao.findByMerCustId(merId, merCustId);
	     if(list != null  && !list.isEmpty()) {
	         return list.get(0);
	     }
	     return null;
	 }
}
